package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussFarmingActivityDao;
import io.gbznt.modules.buss.entity.BussFarmingActivityEntity;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.service.BussFarmingActivityService;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysDictService;
import io.gbznt.modules.sys.service.SysUserService;


@Service("bussFarmingActivityService")
public class BussFarmingActivityServiceImpl extends ServiceImpl<BussFarmingActivityDao, BussFarmingActivityEntity> implements BussFarmingActivityService {
    @Autowired
    private SysUserService sysuser;
    @Autowired
    private BussFarmlandInfoService binfo;
    @Autowired
    private SysDeptService dept;
    @Autowired
    private SysDictService dict;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	//搜索条件---活动名称--田块---农事类型
    	String hdmc = (String) params.get("name");
    	String tk = (String) params.get("tk");
    	String nslx = (String) params.get("nslx");

		IPage<BussFarmingActivityEntity> page = this.page(
                new Query<BussFarmingActivityEntity>().getPage(params),
                new QueryWrapper<BussFarmingActivityEntity>()
                .like(StringUtils.isNotBlank(hdmc), "name", hdmc)
                .eq(StringUtils.isNotBlank(tk), "farmland_info_id", tk)
                .eq(StringUtils.isNotBlank(nslx), "type_dict", nslx)
				// 个人数据权限控制
				.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
						"dept_id", dept.getPermissionDeptIdsArr(true,true))
				.orderByDesc("create_time")
        );
        for (BussFarmingActivityEntity ba : page.getRecords()) {
			//负责人
        	String fzr = ba.getHandler();
        	SysUserEntity sysuseren = sysuser.getById(fzr);
        	if(sysuseren!=null){
        	  ba.setHandlername(sysuseren.getName());	
        	 // ba.setDeptIdname(sysuseren.getDeptName());
        	}
        	//田块名称
        	BussFarmlandInfoEntity bent =  binfo.getById(ba.getFarmlandInfoId());
        	if(bent!=null){
        	   ba.setFarmlandInfoIdname(bent.getName());
        	}
        	//部门(基地)
        	String bm = ba.getDeptId();
        	SysDeptEntity sdept = dept.getById(bm);
        	if(sdept!=null){
        	  ba.setDeptIdname(sdept.getName());	
        	 // ba.setDeptIdname(sysuseren.getDeptName());
        	}
        	//农事类型
        	String ns = ba.getTypeDict();
        	SysDictEntity diceen = dict.getById(ns);
        	if(diceen!=null){
        	  ba.setTypeDictname(diceen.getName());	
        	 // ba.setDeptIdname(sysuseren.getDeptName());
        	}
        	
        	
		}

        return new PageUtils(page);
    }

}
